package cn.learn.athena.global;

import cn.learn.athena.exception.AthenaRuntimeException;
import cn.learn.athena.exception.PageNotFoundException;
import cn.learn.athena.exception.ValidateException;
import cn.learn.athena.util.Result;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zlj
 * @Date 2018/11/7
 * @des
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AthenaRuntimeException.class)
    @ResponseBody
    public Result handleAthenaRuntimeException(HttpServletRequest request, Exception e) {
        Result<Object> result = new Result<>();
        result.setErr(e.getMessage());
        return result ;
    }

    @ExceptionHandler(PageNotFoundException.class)
    public ModelAndView handlePageNotFound(HttpServletRequest request , Exception e , Model model) {
        String url = request.getRequestURI();
        model.addAttribute("url",url);
        model.addAttribute("info",e.getMessage());
        return new ModelAndView("exception/404") ;
    }
}
